package srx.open.com.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;

import java.util.ArrayList;
import java.util.List;

import srx.open.com.R;

/**
 *  排序: 推荐  热门  最新 菜单 adapter.
 */
public class TopRecyclerAdapter extends RecyclerView.Adapter<TopRecyclerAdapter.TopViewHolder> {

    public int mSelectedItem = 0;

    private List<String> topList = new ArrayList<String>() {
        {
            add("推荐");
            add("热门");
            add("最新");
        }
    };

    public TopRecyclerAdapter() {
    }

    public TopRecyclerAdapter(List<String> topList) {
        this.topList = topList;
    }

    @Override
    public TopViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.listitem_nearby_select, parent, false);
        TopViewHolder holder = new TopViewHolder(v);
        return holder;
    }

    @Override
    public void onBindViewHolder(TopViewHolder topViewHolder, int pos) {
        topViewHolder.name_rbtn.setText(topList.get(pos));
        topViewHolder.name_rbtn.setChecked(pos == mSelectedItem);
    }

    @Override
    public int getItemCount() {
        return topList.size();
    }

    public class TopViewHolder extends RecyclerView.ViewHolder {
        public RadioButton name_rbtn;
        public TopViewHolder(final View itemView) {
            super(itemView);
            name_rbtn = (RadioButton) itemView.findViewById(R.id.name_rbtn);
            View.OnClickListener clickListener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mSelectedItem = getAdapterPosition();
                    notifyDataSetChanged();
                }
            };
            itemView.setOnClickListener(clickListener);
            name_rbtn.setOnClickListener(clickListener);
        }
    }

}
